package com.may.wlrj.services;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;

import com.may.wlrj.dao.SysDAO;
import com.may.wlrj.dao.UserDAO;
import com.may.wlrj.entity.UserEntity;
import com.may.wlrj.util.VerifyLicense;

@Service
public class CircleService {
	private Logger log = Logger.getLogger(CircleService.class);
	private UserDAO userDao;

    @Autowired
    public void setUserDao(UserDAO userDao){
        this.userDao =userDao;
    }
    
    private SysDAO sysDao;

    @Autowired
    public void setSysDao(SysDAO sysDao){
        this.sysDao =sysDao;
    }
	//定时执行的方法
    public void execute(){
    	/*log.info("[X]----------------------->>>验证license...");
    	try{
    		Date last = sysDao.getlastlogDate();
    		VerifyLicense vLicense = new VerifyLicense();
    		vLicense.setParam("pram.properties");
    		//验证证书
    		vLicense.verify(last);
    	}catch(EmptyResultDataAccessException er){
    		log.error("[X]----------------------->>>验证license异常...");
    	}
		*/
    	Pswcheck();
    }
    
    private void Pswcheck(){
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	Date date = null;
    	String val = sysDao.findByName("NeedPwdChange");
    	if(!val.equalsIgnoreCase("") && !val.equalsIgnoreCase("0")){
    		try{
    			int cday = Integer.parseInt(val);
    			List<UserEntity> userlist = sysDao.findUserAll();
    			if(userlist != null){
    				for(UserEntity user : userlist){
    					try {
    						if(user.getUsername().equalsIgnoreCase("hzwlprevail")){
    							continue;
    						}
    			            // 注意格式需要与上面一致，不然会出现异常
    						if(!user.getUpdatepswtime().equalsIgnoreCase("")){
    							date = sdf.parse(user.getUpdatepswtime());
    							if(differentDays(date,new Date()) >= cday){
    								if(userDao.setupdateNeedpwd(1, user.getUsername()) == 0){
    									log.error("[X]----------------------->>>更新setupdateNeedpwd失败...");
    								}
    							}
    						}else{
    							if(userDao.setupdateNeedpwd(1, user.getUsername()) == 0){
									log.error("[X]----------------------->>>更新setupdateNeedpwd失败...");
								}
    						}
    			            
    			        } catch (Exception e) {
    			            e.printStackTrace();
    			        }
    				}
    			}
    		}catch(Exception ex){
    			log.error("[X]----------------------->>>NeedPwdChange值解析异常...");
    		}
    		
    	}
    }
    
    public static int differentDays(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);
        
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //不同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年            
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }
            
            return timeDistance + (day2-day1) ;
        }
        else    //同一年
        {
            //System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }


}